#!/bin/sh


grep "Linux" /proc/version
if [ $? -eq 0 ]; then
############################################
### 		Working with Linux
HOMEDIR=/group/project/statmt2/chongde
SRILM=$HOMEDIR/tools/srilm/bin/i686
IRSTLM=$HOMEDIR/tools/irstlm/bin/i686
MOSESDIR=$HOMEDIR/moses
DECODER=$MOSESDIR/moses-cmd/src/moses
BINARIZER=$MOSESDIR/misc/processPhraseTable
############################################
else
grep "CYGWIN" /proc/version
	if [ $? -eq 0 ]; then
############################################
### 		Working with Cygwin
HOMEDIR=/smt
SRILM=$HOMEDIR/tools/srilm/bin/cygwin
IRSTLM=$HOMEDIR/tools/irstlm/bin/i686-pc-cygwin
MOSESDIR=$HOMEDIR/moses
DECODER=$MOSESDIR/moses-cmd/src/moses.exe
BINARIZER=$MOSESDIR/misc/processPhraseTable.exe
############################################
	else
		echo "The system is neither Linux nor Cygwin, exit!"
		exit 1;
	fi
fi

# Global
BASEDIR=$HOMEDIR/lattice_decoding
MOSESSCRIPTS=$HOMEDIR/tools/moses-scripts
WORKDIR=$HOMEDIR/tmp/mini_ltc
DATADIR=$BASEDIR/mini

TR_CLEAN_CHI=$WORKDIR/corpus/training.clean.chi
TR_CLEAN_ENG=$WORKDIR/corpus/training.clean.eng
TR_LOW_CHI=$WORKDIR/corpus/training.low.chi
TR_LOW_ENG=$WORKDIR/corpus/training.low.eng
TR_LOW_STEM=$WORKDIR/corpus/training.low
TR_LM=$HOMEDIR/tools/lm/training.blm
TR_PT=$WORKDIR/model/phrase-table.0-0.gz
TU_CHI=$DATADIR/tuning.chi
TU_ENG=$DATADIR/tuning.eng
MOSES_INI_MODEL=$WORKDIR/model/moses.ini
MOSES_INI_REUSED=$WORKDIR/tuning/moses.weight-reused.ini
MOSES_INI_TEST=$WORKDIR/eval/filtered/moses.ini
TE_FILTERED=$WORKDIR/eval/filtered
TE_OUT=$WORKDIR/eval/output
TE_OUT_DETOK=$WORKDIR/eval/output.detok
TE_OUT_SGM=$WORKDIR/eval/output.sgm

STEP=1
echo "----------------------------------------------------------------"
echo "Step $STEP : make dirs"
cd $BASEDIR

#rm -rf $WORKDIR

mkdir -p $WORKDIR/corpus
mkdir -p $WORKDIR/model
mkdir -p $WORKDIR/tuning
mkdir -p $WORKDIR/eval

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP: clean corpus"
$MOSESSCRIPTS/training/clean-corpus-n.perl $DATADIR/training chi eng $WORKDIR/corpus/training.clean 1 80

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : lowercase corpus"
$BASEDIR/scripts/lowercase.perl < $TR_CLEAN_CHI > $TR_LOW_CHI
$BASEDIR/scripts/lowercase.perl < $TR_CLEAN_ENG > $TR_LOW_ENG

#STEP=`expr $STEP + 1`
#echo "----------------------------------------------------------------"
#echo "Step $STEP : create lm"
#if [ ! -f $TR_LM ]; then
#  echo "$SRILM/ngram-count -order 5 -interpolate -kndiscount -text $TR_LOW_ENG -lm $TR_LM"
#  $SRILM/ngram-count -order 5 -interpolate -kndiscount -text $TR_LOW_ENG -lm $TR_LM
#  $IRSTLM/compile-lm $TR_LM $TR_LM_BIN
#else
#	echo "Skipped"
#fi

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : training with giza++"
$MOSESSCRIPTS/training/train-factored-phrase-model.perl -scripts-root-dir $MOSESSCRIPTS -root-dir $WORKDIR -corpus $TR_LOW_STEM -f chi -e eng -alignment grow-diag-final-and -reordering msd-bidirectional-fe -lm 0:5:$TR_LM:1

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : copy moses.ini"
cp $MOSES_INI_MODEL $MOSES_INI_REUSED


STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : filter test set"
if [ -d $TE_FILTERED ]; then
  	echo " "
	#rm -rf $TE_FILTERED
fi
## need modification for lattice input
### this line filter the phtable, it's not ready
$MOSESSCRIPTS/training/filter-and-binarize-model-given-input.pl $MOSESDIR/misc/processPhraseTable $TE_FILTERED $MOSES_INI_REUSED $DATADIR/nor_test.chi

### so we just binarize the whole table
#gunzip <$TR_PT sort -T $TE_FILTERED 
#gunzip <$TR_PT | sort -T $TE_FILTERED | $BINARIZER -ttable 0 0 - -nscores 5 -out $TE_FILTERED/phrase-table.0-0-0

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : decode test set"
#$DECODER  -drop-unknown  -config $MOSES_INI_TEST -input-file $DATADIR/nor_test.chi > $TE_OUT
$DECODER  -drop-unknown -inputtype 1 -config $MOSES_INI_TEST -input-file $DATADIR/cfn_test.chi > $TE_OUT
#$DECODER  -drop-unknown -inputtype 2 -config $MOSES_INI_TEST -input-file $DATADIR/ltc_test.chi > $TE_OUT

echo ""
echo "----------------------------------------------------------------"
echo "---------------          Translation       ---------------------"
echo "----------------------------------------------------------------"
cat $TE_OUT


